package com.younion.business.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.younion.business.ICustomerService;
import com.younion.common.paginator.Paginator;
import com.younion.dao.CustomerDAO;
import com.younion.dao.MemberCardDAO;
import com.younion.dao.RechargeLogDAO;
import com.younion.vo.CustomerVO;
import com.younion.vo.RechargeLogVO;

import cpcns.component.common.base.BaseException;
import cpcns.jhsks.util.MapCollectionUtils;

 
public class CustomerServiceImpl implements ICustomerService {

	protected static final Log logger = LogFactory.getLog(CustomerServiceImpl.class);

	private CustomerDAO customerDAO;
	
	@Autowired
	private RechargeLogDAO rechargeLogDAO;
	
	public Collection getAll() throws Exception {
		return customerDAO.selectAll();
	}

	public CustomerVO getById(String id) throws Exception {
		if (StringUtils.isEmpty(id) || Integer.parseInt(id)<1)
			return null;
		try {
			Map paramMap = new HashMap();
			paramMap.put("id", new Integer(id));
			Object obj = customerDAO.selectById(paramMap);
			return obj==null?null:(CustomerVO)obj;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public CustomerVO get(String id) error occur:",e);
		}
	}
	public Collection getByMap(Map paramMap) throws BaseException {
		if(CollectionUtils.isEmpty(paramMap)){
			return null;
		}
		try{
			return customerDAO.selectByMap(paramMap);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public Collection getByMap(Map map) error occur:",e);
		}
	}

	public int getCountByMap(Map paramMap) throws BaseException {
		try {
			return customerDAO.selectCountByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()
					+ " >> public Collection getCountByMap(Map map) error occur:", e);
		}
	}

	public List get(Map paramMap, Paginator paginator) throws Exception {
		try {
			Map map = new HashMap();
			map.put("paramMap", paramMap);
			return customerDAO.selectForPage(map, paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public Page (Map paramMap, Paginator paginator) error occur:",e);
		}
	}

	public CustomerVO save(CustomerVO voObject) throws Exception {
		if (voObject == null)
			return null;
		try {
			Object id = null;
			id = voObject.getId();
			Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(voObject));
			if (id != null) {
				customerDAO.updateByMap(paramMap);
				return voObject;
			} else {
				return (CustomerVO)customerDAO.insertByVo(voObject);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public CustomerVO save(CustomerVO voObject) error occur:",e);
		}
	}

	public Collection save(Collection voList) throws Exception {
		if (CollectionUtils.isEmpty(voList))
			return null;
		try {
			Iterator it = voList.iterator();
			while (it.hasNext()) {
				CustomerVO data = (CustomerVO) it.next();
				data = save(data);
			}
			return voList;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public Collection save(Collection voList) error occur:",e);
		}
	}

	public boolean remove(List ids) throws Exception {
		if (CollectionUtils.isEmpty(ids))
			return false;
		try{
			customerDAO.deleteByIds(ids);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public boolean remove(List ids) error occur:",e);
		}
	}

	public boolean removeByMap(Map paramMap) throws Exception {
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try {
			customerDAO.deleteByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public boolean removeByMap(Map paramMap) error occur:",e);
		}
	}
	
	public boolean updateCooperationStaus(Map paramMap)throws Exception{
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try{
			customerDAO.relieveByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public boolean relieve(Map paramMap) error occur:",e);
		}
	 }
	
	public boolean updateCustomerMoney(Map map, List<RechargeLogVO> rechangeLogs) throws Exception {
		if (CollectionUtils.isEmpty(map))
			return false;
		try{
			rechargeLogDAO.batchInsert(rechangeLogs);
			customerDAO.recharge(map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public boolean recharge(Map map) error occur:",e);
		}
	}
	
	public int selectMaxTop() throws BaseException {
		try {
			return this.customerDAO.selectMaxTop();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()
					+ " >> public int selectMaxTop() error occur:", e);
		}
	}
	
	public void updateTopById(Map map) throws BaseException {
		try {
			this.customerDAO.updateTopById(map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()
					+ " >> public void updateTopById(Map map) error occur:", e);
		}
	}
	
	public List selectByIds(List ids) throws Exception {
		return customerDAO.selectByIds(ids);
	}
	
	public boolean removeByIdsRelated(List<String> list) throws BaseException {
		if (CollectionUtils.isEmpty(list))
			return false;
		try {
			customerDAO.deleteByIdsRelated(list);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CustomerServiceImpl.class.getName()+" >> public boolean deleteByObjs(List<String> list) error occur:",e);
		}
	}
	/**
	 * @param service
	 */
	public void setCustomerDAO(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	public RechargeLogDAO getRechargeLogDAO() {
		return rechargeLogDAO;
	}

	public void setRechargeLogDAO(RechargeLogDAO rechargeLogDAO) {
		this.rechargeLogDAO = rechargeLogDAO;
	}

	public CustomerDAO getCustomerDAO() {
		return customerDAO;
	}
	
	
}